/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.psc.ejb;

import com.mycompany.psc.entities.Aluminium;
import com.mycompany.psc.entities.Carpenter;
import com.mycompany.psc.entities.MetalDoors;
import com.mycompany.psc.entities.Mobilization;
import com.mycompany.psc.entities.Projects;
import com.mycompany.psc.entities.Resource;
import com.mycompany.psc.entities.SiteExpenses;
import com.mycompany.psc.entities.TenderItems;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.validation.ConstraintViolation;

/**
 *
 * @author Ahmed
 */
@Stateless
public class ProjectsManager {

    @PersistenceContext(unitName = "com.mycompany_PSC_war_1.0-SNAPSHOTPU")
    EntityManager em;

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    public Projects SaveProject(String name, String desc) {

        try {
            Projects p = new Projects();
            p.setName(name);
            p.setDescription(desc);
            em.persist(p);
            return p;
        } catch (Exception e) {
            Logger.getGlobal().log(Level.SEVERE, e.getLocalizedMessage());
            return null;
        }

    }

    public List<Projects> listAllProjects() {
        TypedQuery<Projects> query = em.createNamedQuery("Projects.findAll", Projects.class);
        return query.getResultList();
    }

    public List<Projects> listOtherProjects(Projects p1) {
        TypedQuery<Projects> query = em.createNamedQuery("Projects.findOther", Projects.class);
        query.setParameter("id", p1.getId());
        return query.getResultList();
    }

    public void updateProject(Projects p) {
        em.merge(p);
    }

    public void deleteProject(Projects p) {
        TypedQuery<Resource> query = em.createNamedQuery("Resource.findByProjectId", Resource.class);
        query.setParameter("projectId", p.getId());
        for (Resource r : query.getResultList()) {
            em.remove(r);
        }
        Projects removeable = em.getReference(Projects.class, p.getId());
        em.remove(removeable);
    }

    public Projects getByName(String name) {
        TypedQuery<Projects> query = em.createNamedQuery("Projects.findByName", Projects.class);
        query.setParameter("name", name);
        return query.getResultList().get(0);
    }

    public void updateTenderItems(Projects source, Projects target) {
        try {
            for (Carpenter c : source.getCarpenterList()) {
                Carpenter newCarpenter = c.createCopy(target);
                target.getCarpenterList().add(newCarpenter);
                em.merge(target);
                c.copyChildren(newCarpenter);
                em.merge(newCarpenter);
            }
            for (Aluminium a : source.getAluminiumList()) {
                Aluminium newAlluminuim = a.createCopy(target);
                target.getAluminiumList().add(newAlluminuim);
                em.merge(target);
                a.copyChildren(newAlluminuim);
                em.merge(newAlluminuim);
            }
            for (MetalDoors a : source.getMetalDoorsList()) {
                MetalDoors newMetalDoors = a.createCopy(target);
                target.getMetalDoorsList().add(newMetalDoors);
                em.merge(target);
                a.copyChildren(newMetalDoors);
                em.merge(newMetalDoors);
            }
            for (Mobilization a : source.getMobilizationList()) {
                Mobilization newMobilization = a.createCopy(target);
                target.getMobilizationList().add(newMobilization);
                em.merge(target);
                a.copyChildren(newMobilization);
                em.merge(newMobilization);
            }
            for (SiteExpenses a : source.getSiteExpensesList()) {
                SiteExpenses newSiteExpenses = a.createCopy(target);
                target.getSiteExpensesList().add(newSiteExpenses);
                em.merge(target);
                a.copyChildren(newSiteExpenses);
                em.merge(newSiteExpenses);
            }
            for (TenderItems a : source.getTenderItemsList()) {
                TenderItems newTenderItems = a.createCopy(target);
                target.getTenderItemsList().add(newTenderItems);
                em.merge(target);
                a.copyChildren(newTenderItems);
                em.merge(newTenderItems);
            }

        } catch (javax.validation.ConstraintViolationException e) {
            for (ConstraintViolation v : e.getConstraintViolations()) {
                System.err.println("violated" + v.getRootBean());
            }
        }

    }

}
